package com.bawei.module_home.ui.area

import android.util.Log
import android.widget.RadioButton
import androidx.activity.viewModels
import com.alibaba.android.arouter.facade.annotation.Route
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_storage.AppMMKV
import com.bawei.module_home.R
import com.bawei.module_home.databinding.ActivityAddAreaBinding
import com.bawei.module_home.http.HomeServiceViewModel
import com.bawei.module_home.model.addAreaBody
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = Router.ADD_AREA_PAGE)
class AddAreaActivity : BaseActivity<HomeServiceViewModel, ActivityAddAreaBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_add_area
    val mViewModel: HomeServiceViewModel by viewModels()

    var userName: String=""
    lateinit var status: String
    override fun initView() {
        super.initView()

        userName= AppMMKV.username
    }

    override fun initData() {
        mBinding.group.setOnCheckedChangeListener({ group, checkedId ->
            val selectedRadioButton=findViewById<RadioButton>(checkedId)
            if(checkedId!=-1){
                status=selectedRadioButton.text.toString()

            }
            when(checkedId){
                R.id.status_a -> status
                R.id.status_b -> status
                R.id.status_c -> status
            }
            Log.i("AddAreaActivity", "initView: "+status)
        })

        mBinding.b1.setOnClickListener({

            val body = addAreaBody(
                areaId = 1,
                areaName = mBinding.areaName.text.toString(),
                id = 0,
                licensePlate = mBinding.licensePlate.text.toString(),
                remarks = mBinding.remarks.text.toString(),
                spaceNumber = mBinding.spaceNumber.text.toString(),
                spaceStatus = status,
                spaceType = mBinding.spaceType.text.toString(),
                userId = AppMMKV.userId,
                userName = userName
            )
            mViewModel.addArea(body)
        })

    }

    override fun initListener() {
        mViewModel.addAreaState.observeWithLifecycle(this){
            when(it){
                Empty -> {}
                is Failed -> {}
                Loading ->  {}
                Success -> {
                    finish()
                }
            }
        }

    }

}